import json
import math
import random
import time

from tqdm import tqdm

import requests

# 目标url
url = "https://mp.weixin.qq.com/cgi-bin/appmsg"
cookie = "appmsglist_action_2397764011=card; ua_id=fiscIldUR4i4ooU7AAAAAK_Fv9OwoxWLMKnws9kyEQs=; cert=cLdu3snkcYCHovPqPGZobXxZgjOV23x1; sig=h01b217310a38d79efcaa0310072f1be7cc96b8125becb009e0e370bb910ecc4a7901280f771a8c03ab; mm_lang=zh_CN; media_ticket_id=2397764011; _qimei_q36=76d1329a0ddbcd131481cddd300011e17a09; _qimei_uuid42=186160d0d021003d0c7b922d6d9b1229325583f23f; RK=bQdk/ycTYV; ptcz=88583b179524eb33b694cb10f490db1a42054fb9386cc298ced3c3427feb38fd; pgv_pvid=4923200134; _ga=GA1.2.1248120991.1698026255; poeqqcomrouteLine=news_a202010118poena_a202010118poena_a202010118poena_a202010118poena_a202010118poena_a202010118poena; eas_sid=r1a7n1k9C2E230m4K5c7O2F3Y7; ied_qq=o0694943221; tokenParams=%3F%25E9%25AC%25BC%25E7%25AC%25A6%25E5%258F%2581%25E9%2580%259A%26S25%25E9%259D%2592%25E9%259B%2589%26i%3D0%26search%3Dclass%253DChieftain%26sai%3Ds25_normal; pgv_info=ssid=s9916862744&pgvReferrer=; _qpsvr_localtk=0.8952637051294812; qq_domain_video_guid_verify=48902a554c4bc94c; _qimei_fingerprint=7bb3cb1c49371d7787c2a23f10ad943c; __root_domain_v=.weixin.qq.com; _qddaz=QD.114223625403856; suid=user_0_DJyPdTrRbbSp3; media_ticket=7cccdd861c19f5cd6dc9c8b00db74c4360384900; tvfe_boss_uuid=c9ba1fc120dea464; o_cookie=694943221; vversion_name=8.2.95; video_omgid=48902a554c4bc94c; wxuin=36584556450464; _qimei_q32=5bf9e14c15c94f360a254413d89dcbb3; _qimei_h38=59a996ac1eeb09f4c3b02cf00200000221770e; ts_uid=5796398812; skey=@shSekRKwm; uin=o694943221; rewardsn=; wxtokenkey=777; mmad_session=05d5dedcb70c802154fee115440fdfd81201dafc40ed4b5a591d99fdcc45c287f9dad9640de435cc976b18fa49f09c12b81e8c9a65cfb8115f12ae3c3f353e21a8fe2b87e50fbe3a9810f4bad7e0cefcd56e06e4158a02787db7f16cf87a8b1111de1c56c245721266e7088080fefde3; _clck=2397764011|1|ft1|0; uuid=afc57723e49c1c24796af8cdb81eb74d; rand_info=CAESIAgfl0t4O1R1wvSha0OaoP4PfWMTJswO8Q6smoh7Q8RI; slave_bizuin=2397764011; data_bizuin=2397764011; bizuin=2397764011; data_ticket=GNgjczCPIB5npJUpd/8XyqqvqyDaPWIoeZKSnDl23iPbwBPbVh5KAEQoi9hJJnPu; slave_sid=dmJsMldRRWxPbF9xRHhDejVIWjdUdVBtTDJhWEFnRmg2ZmlKVFg3UjhpSmlCQWJzWjI4RmhKMjBZSVlRWXAxN2t2R3RCdFhTcmVRZGZzMEdYYUQxbF9KRGVyc3FDWFhza1NWMkhwcU5BRUFFcGRHTlNkNHFYcERZeHhnWVFzZTRwYVM2dm5CQ25XZU5qWFVa; slave_user=gh_56b58dda9d34; xid=f9a032ec645d6ccd281cff10db249262; _clsk=vnzpux|1738297966691|2|1|mp.weixin.qq.com/weheat-agent/payload/record"

headers = {
    "Cookie": cookie,
    "User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.75 Mobile Safari/537.36",
}

data = {
    "token": "1603736543",
    "lang": "zh_CN",
    "f": "json",
    "ajax": "1",
    "action": "list_ex",
    "begin": "0",
    "count": "5",
    "query": "",
    "fakeid": "", # 自己的号，设置为空
    "type": "9",
}


def get_total_count():
    content_json = requests.get(url, headers=headers, params=data,verify=False).json()
    count = int(content_json["app_msg_cnt"])
    return count


def get_content_list(count, fakeid='MzkwODc1NTQ0Ng==', per_page=5):
    page = int(math.ceil(count / per_page))
    content_list = []
    for i in tqdm(range(page), desc="获取文章列表"):
        data["begin"] = i * per_page
        data["fakeid"] = fakeid
        content_json = requests.get(url, headers=headers, params=data,verify=False).json()
        content_list.extend(content_json["app_msg_list"])
        time.sleep(random.randint(5, 10))
        # # 保存成json
        # with open("content_list.json", "w", encoding="utf-8") as f:
        #     json.dump(content_list, f, ensure_ascii=False, indent=4)
    return content_list

def get_account_list(dir):
    account_list = []
    with open(dir, 'r', encoding="utf-8") as file_to_read:
        while True:
            line = file_to_read.readline()
            if not line:
                break
            line = line.strip().split()
            account_list.append(line)
    return account_list


if __name__ == '__main__':
    # path = 'account_list.txt'
    # linklist = get_account_list(path)
    get_content_list(10)